var express = require("express");
var router = express.Router();
var upload = require('./fileuploads');
var Userinformation = require('../models/userinformation');
var Post = require('../models/post')
/* GET upload listing. */
router.get('/', function(req, res, next) {
    res.render('testuploadfile');
});


var checkFiled =  function checkUnfined(value){
    if(typeof(value)=='undefined'){
        return "";
    }
    return value;
}



//新增用户
router.post('/adduser',upload.single('avatar'),function (req,res,next) {
    var body = req.body;
    console.log(body);

    if(body){
        var username = checkFiled(body.username);
        if(username==""){
            var dataError = {
                status: '0',
                msg: '姓名参数不全'
            };
            res.end(JSON.stringify(dataError));
        }
        var gender = checkFiled(body.gender);
        if(gender==""){
            gender="";
        }
        var mobile = body.mobile;
        if(mobile==""){
            var dataError = {
                status: '0',
                msg: '手机参数不全'
            };
            res.end(JSON.stringify(dataError));
        }
        var birth = checkFiled(body.birth);
        if(birth==""){
            birth="";
        }
        var age = checkFiled(body.age);
        if(age==""){
            age="";
        }



        var filename = "";

        if(req.file){
            filename = checkFiled(req.file.filename);
            console.log(req.file);
            console.log(req.body);
        }


        var newUserInfor = new Userinformation({
            username:username,
            gender:gender,
            mobile:mobile,
            birth:birth,
            age:age,
            headurl:filename,
            createtime:Date.now(),
            updatetime:Date.now(),
            isdelete:'0'
        })



        var userid = checkFiled(body.userid);
        if(userid==""){
            var dataSuccess = {
                status: '200',
                msg: '用户添加成功',
                data: {
                    username:username,
                    gender:gender,
                    mobile:mobile,
                    birth:birth,
                    age:age,
                    headurl:filename
                }
            };
            Userinformation.findOne({username:username},function (err,userinfor) {
                if(userinfor){
                    var dataError = {
                        status: '0',
                        msg: '姓名已存在'
                    };
                    res.end(JSON.stringify(dataError));
                }
                else if(err){
                    var dataError = {
                        status: '0',
                        msg: err.message
                    };
                    res.end(JSON.stringify(dataError));
                }
                else {
                    Userinformation.findOne({mobile:mobile},function (err,userinfor) {
                        if(userinfor){
                            var dataError = {
                                status: '0',
                                msg: '手机号已存在'
                            };
                            res.end(JSON.stringify(dataError));
                        }
                        else if(err){
                            var dataError = {
                                status: '0',
                                msg: err.message
                            };
                            res.end(JSON.stringify(dataError));
                        }
                        else {

                            newUserInfor.save(function () {
                                res.end(JSON.stringify(dataSuccess));

                            });
                        }


                    });
                }


            });




        }
        else{
            var dataSuccess = {
                status: '200',
                msg: '用户修改成功',
                data: {
                    username:username,
                    gender:gender,
                    mobile:mobile,
                    birth:birth,
                    age:age,
                    headurl:filename
                }
            };

            var whereStr = {"_id":userid};
            var updateStr = {
                $set:
                    {
                        username:username,
                        gender:gender,
                        mobile:mobile,
                        birth:birth,
                        age:age,
                        headurl:filename

            }};

            Userinformation.update(whereStr,updateStr,function (err) {
                if(err){
                    var dataError = {
                        status: '0',
                        msg: err.message
                    };
                    res.end(JSON.stringify(dataError));
                }
                else {
                    var dataSuccess = {
                        status: '200',
                        msg: '修改成功'
                    };
                    res.end(JSON.stringify(dataSuccess));
                }

            });
        }
    }
    else {
        var dataError = {
            status: '0',
            msg: '参数不全'
        };
        res.end(JSON.stringify(dataError));
    }



});


router.post('/addpost',upload.single('avatar'),function (req,res,next) {
    var body = req.body;
    console.log(body);

    if(body){
        var content = checkFiled(body.content);
        if(content==""){
            var dataError = {
                status: '0',
                msg: '动态内容参数不全'
            };
            res.end(JSON.stringify(dataError));
        }

        var userid = checkFiled(body.userid);
        if(userid==""){
            var dataError = {
                status: '0',
                msg: '关联用户id参数不全'
            };
            res.end(JSON.stringify(dataError));
        }

        var filename = "";

        if(req.file){
            filename = checkFiled(req.file.filename);
            console.log(req.file);
            console.log(req.body);
        }


        var newPosts = new Post({
            content:content,
            user:userid,
            coverimageurl:filename,
            createtime:Date.now(),
            updatetime:Date.now(),
            isdelete:'0'
        })



        var postid = checkFiled(body.postid);
        if(postid==""){
            var dataSuccess = {
                status: '200',
                msg: '动态添加成功',
                data: {
                    content:content,
                    user:userid,
                    coverimageurl:filename,
                    createtime:newPosts.createtime,
                    updatetime:newPosts.updatetime
                }
            };
            newPosts.save(function () {
                res.end(JSON.stringify(dataSuccess));

            });

        }
        else {
            var dataSuccess = {
                status: '200',
                msg: '动态修改成功',
                data: {
                    content: content,
                    user: userid,
                    coverimageurl: filename,
                    updatetime: newPosts.updatetime
                }
            };

            var whereStr = {"_id": postid};
            var updateStr = {
                $set:
                    {
                        content: content,
                        user: userid,
                        updatetime: newPosts.updatetime,
                        coverimageurl: filename

                    }
            };

            Post.update(whereStr, updateStr, function (err) {
                if (err) {
                    var dataError = {
                        status: '0',
                        msg: err.message
                    };
                    res.end(JSON.stringify(dataError));
                }
                else {
                    var dataSuccess = {
                        status: '200',
                        msg: '修改成功'
                    };
                    res.end(JSON.stringify(dataSuccess));
                }

            });
        }
    }
    else {
        var dataError = {
            status: '0',
            msg: '参数不全'
        };
        res.end(JSON.stringify(dataError));
    }



});


module.exports = router;